<template>
  <div class="container">
    <el-row :span="6">
      <el-card shadow="always" class="cardd">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="目录名称">
            <el-input
              v-model="params.directoryName"
              placeholder="请输入"
            ></el-input>
          </el-form-item>

          <el-form-item label="状态" style="margin-left: 40px">
            <el-select v-model="params.state" placeholder="请选择">
              <el-option label="启动" value="0"></el-option>

              <el-option label="禁用" value="1"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button size="small" @click="eliminate">清除</el-button>

            <el-button type="primary" size="small" @click="getList()"
              >搜索</el-button
            >

            <el-link
              v-show="this.$route.query.id"
              icon="el-icon-arrow-left"
              style="margin-left: 300px; margin-right: 10px"
              :underline="false"
              @click="$router.back()"
              >返回学科</el-link
            >
          </el-form-item>
        </el-form>

        <el-button type="success" size="small" @click="addlabel" class="sousuo"
          ><i class="el-icon-edit"></i>新增标签</el-button
        >
        <div class="total">
          <i class="el-icon-info"></i>
          <span>数据一共{{ counts }}条</span>
        </div>

        <template>
          <el-table :data="items" style="width: 100%">
            <el-table-column type="index" label="序号" width="80">
            </el-table-column>

            <el-table-column prop="subjectID" label="所属学科">
              <template #default="{ row }">
                {{ subidandname(row.subjectID) }}
              </template>
            </el-table-column>

            <el-table-column prop="directoryName" label="目录名称">
            </el-table-column>

            <el-table-column prop="username" label="创建者"> </el-table-column>

            <el-table-column prop="addDate" label="创建日期">
              <template #default="{ row }">
                {{ formatTime(row.addDate) }}
              </template>
            </el-table-column>

            <el-table-column prop="totals" label="面试题数量">
            </el-table-column>

            <el-table-column prop="state" label="状态">
              <template #default="{ row }">
                {{ formatSate(row.state) }}
              </template>
            </el-table-column>

            <el-table-column fixed="right" label="操作">
              <template #default="{ row }">
                <el-button type="text" size="small" @click="states(row)">{{
                  row.state == 1 ? "禁用" : "启用"
                }}</el-button>

                <el-button
                  type="text"
                  size="small"
                  :disabled="row.state === 1"
                  @click="modify(row.id)"
                  >修改</el-button
                >

                <el-button
                  type="text"
                  size="small"
                  @click="delcate(row.id)"
                  :disabled="row.state === 1"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </template>

        <!-- 分页 -->
        <el-pagination
          background
          :current-page="params.page"
          :page-sizes="[2, 3, 5, 10]"
          :page-size="params.pagesize"
          @current-change="pageChange"
          @size-change="pageSize"
          layout="prev, pager, next, sizes,jumper"
          :total="counts"
          style="margin-top: 20px; text-align: right"
        >
        </el-pagination>
      </el-card>
    </el-row>
    <!-- 弹出框 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="30%"
      @close="close"
      @open="openDialog"
    >
      <template>
        <el-form ref="addForm" label-width="100px" :model="form" :rules="rules">
          <el-form-item
            v-if="!this.$route.query.id"
            label="所属学科"
            prop="directoryName"
          >
            <el-select
              v-model="form.subjectID"
              style="width: 100%"
              placeholder="请选择"
            >
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="subjectID">
            <el-input
              v-model="form.directoryName"
              placeholder="请输入目录名称"
              style="width: 100%"
            ></el-input>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="close">取 消</el-button>
          <el-button type="primary" @click="confirm">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { list, add, remove, changeState, detail, update } from '@/api/hmmm/directorys'
import { simple } from '@/api/hmmm/subjects'
export default {
  name: 'DirecTorys',
  data () {
    return {
      map: {},
      formstate: { // 禁用，启用
        id: null,
        state: null
      },
      form: { // 表单项
        directoryName: '',
        subjectID: '',
        id: null
      },
      dialogVisible: false, // 弹框显示与隐藏
      items: [], // 表格项数据
      options: [], // 表单所属学科下拉框数据
      counts: null, // 总条数
      dialogTitle: '', // 弹窗标题
      params: { // 目录列表数据
        page: 1,
        subjectID: null,
        pagesize: 10,
        directoryName: '',
        state: null
      },
      rules: { // 表单项数据验证
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ],
        subjectID: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    this.getList()// 渲染列表
  },
  computed: {
    subject () {
      return this.$route.query || {}
    }
  },
  watch: {
    '$route.query': function () {
      this.getList()
    }
  },
  methods: {
    async getList () { // 调用目录列表
      this.params.subjectID = this.subject.id || null
      const res = await list(this.params)

      // console.log(res)

      this.items = res.data.items
      this.counts = res.data.counts
      // console.log(this.items)
      this.items.map(item => { // 所属学科枚举
        if (item.subjectID) {
          // map1.push({})
          this.map[item.subjectID] = item.subjectName
        }
      })
      // this.state = res.data.items.state
    },
    pageChange (page) { // 改变当前页数
      this.params.page = page
      // console.log(page)
      this.getList()
    },
    pageSize (page) {
      this.params.pagesize = page
      this.getList()
    },
    formatTime (value) { // 时间格式化
      return dayjs(value).format('YYYY-MM-DD HH:mm:ss')
    },
    addlabel () { // 新增打开弹出框
      this.dialogVisible = true
      this.dialogTitle = '新增目录'
    },
    async openDialog () { // 弹出框所属学科回显
      const res = await simple()
      // console.log(res.data)
      this.options = res.data
    },
    confirm () { // 确定新增与修改
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          if (this.form.id) {
            await update(this.form)
            this.$message.success('修改成功')
          } else {
            await add(this.form)
            this.$message.success('新增成功')
          }
          this.getList()
          this.$refs.addForm.resetFields()
          this.form = { // 表单项
            directoryName: '',
            subjectID: '',
            id: null
          }
          this.dialogVisible = false
        }
      })
    },
    delcate (id) { // 删除
      this.$confirm('确定要删除吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id: id })
        this.$message.success('删除成功')
        this.getList()
      }).catch(() => {

      })
    },
    formatSate (value) {
      const map = {
        1: '已启用',
        0: '已禁用'
      }
      return map[value]
    },
    async states (row) { // 点击禁用与启用按钮改变状态
      // console.log(row)
      this.formstate.id = row.id
      if (row.state === 1) {
        this.formstate.state = 0
      } else {
        this.formstate.state = 1
      }
      // console.log(this.formstate)
      await changeState(this.formstate)
      this.formstate = {
        id: null,
        state: null
      }
      this.getList()
    },
    eliminate () { // 清除
      this.params = {
        page: 1,
        pagesize: 10,
        directoryName: '',
        state: null
      }
      this.getList()
    },
    async modify (id) { // 修改回显
      this.dialogTitle = '修改目录'
      this.dialogVisible = true
      const res = await detail({ id: id })
      this.form = res.data
    },
    subidandname (value) {
      return this.map[value]
    },
    close () {
      this.form = {
        directoryName: '',
        subjectID: '',
        id: null
      }
      this.dialogVisible = false
    }
  }

}

</script>

<style scoped lang='scss'>
.container .cardd {
  position: relative;

  margin-top: 12px;

  margin-left: 17px;

  margin-right: 17px;
}

.text {
  font-size: 14px;
}

.item {
  padding: 20px 20px;
}

.box-card {
  height: 100%;

  width: 100%;
}
.container .total {
  margin-top: 18px;
  height: 35px;
  background-color: #f4f4f5;
  display: flex;
  justify-content: start;
  align-items: center;
  color: #909399;
  border-radius: 4px;
  font-size: 14px;
  margin-bottom: 20px;
}
.container .total i {
  margin-left: 15px;
  margin-right: 10px;
}
.el-link {
  color: #409eff;
}
.sousuo {
  position: absolute;
  top: 20px;
  right: 20px;
}
</style>
